#!/bin/bash

show_help() {
  echo -e "run openstack cmdline"
  echo -e "Usage:"
  echo -e "$title [options] [-- <openstack options...>]"
  echo -e "Options:"
  echo -e "\t--interactive | -i   interactive mode"
  echo -e "\t--auth-url           keystone auth url"
  echo -e "\t--project            project name"
  echo -e "\t--projectid          project id"
  echo -e "\t--user | -u          user name"
  echo -e "\t--password | -p      password"
  echo -e "\t--help | -h          show this help and exit"
}

cmdopts=$(getopt --longoptions project:,projectid:,user:,password:,auth-url:,title:,interactive,help \
                     --options +hiu:p: -- "$@")

# set positional parameters
eval set -- "$cmdopts"

title=do_osc
project=$OS_PROJECT_NAME
projectid=
username=$OS_USERNAME
password=$OS_PASSWORD
auth_url=$OS_AUTH_URL
interactive=false

while true; do
  case "$1" in
    --title )
        title="$2"
        shift 2;;
    -h | --help )
        show_help
        exit ;;
    -i | interactive )
        interactive=true
        shift;;
    --project )
        project="$2"
        shift 2;;
    --projectid )
        projectid="$2"
        shift 2;;
    -u | --user )
        username="$2"
        shift 2;;
    -p | --password )
        password="$2"
        shift 2;;
    --auth-url )
        auth_url="$2"
        shift 2;;
    -- ) shift; break ;;
    * ) break ;;
  esac
done

#echo -e "os-project-domain-name:  $OS_PROJECT_DOMAIN_NAME"
#echo -e "os-user-domain-name:     $OS_USER_DOMAIN_NAME"
#echo -e "os-project-name:         $project"
#echo -e "os-username:             $username"
#echo -e "os-auth-url:             $auth_url"
#echo -e "os-interface:            $OS_INTERFACE"
#echo -e "os-identity-api-version: $OS_IDENTITY_API_VERSION"
#echo -e "os-region-name:          $OS_REGION_NAME"
#echo -e "os-auth-type:            $OS_AUTH_PLUGIN"
#echo -e "others:                  $*"
#

if $interactive; then
    localcmd="sudo ssh -t root@$controller docker exec -it kolla_toolbox"
else
    localcmd="sudo ssh root@$controller docker exec kolla_toolbox"
fi

projectstr="--os-project-name $project"
if [ -n "$projectid" ]; then
    projectstr="--os-project-id $projectid"
fi

$localcmd \
 openstack --os-compute-api-version 2.79 \
 --os-project-domain-name $OS_PROJECT_DOMAIN_NAME \
 --os-user-domain-name $OS_USER_DOMAIN_NAME \
 $projectstr \
 --os-username $username \
 --os-password $password \
 --os-auth-url $auth_url \
 --os-interface $OS_INTERFACE \
 --os-identity-api-version $OS_IDENTITY_API_VERSION \
 --os-region-name $OS_REGION_NAME \
 --os-auth-type $OS_AUTH_PLUGIN \
 $*

